package hk.ust.comp3111.ustnutrition;

import hk.ust.comp3111.ustnutrition.data.DataAccessor;
import hk.ust.comp3111.ustnutrition.data.DataTableListener;
import hk.ust.comp3111.ustnutrition.data.db.Food;
import hk.ust.comp3111.ustnutrition.data.db.IntakeRecord;
import hk.ust.comp3111.ustnutrition.formula.FoodSuggestion;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;

public class EatNowFragment extends Fragment implements OnItemClickListener {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.eat_now_frag, container, false);
		mGrid = (GridView) root.findViewById(R.id.grid);
		return root;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		DataAccessor.Instance.getRecords().addChangeListener(recordListener);
		DataAccessor.Instance.getFoods().addChangeListener(foodListener);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		DataAccessor.Instance.getRecords().removeChangeListener(recordListener);
		DataAccessor.Instance.getFoods().removeChangeListener(foodListener);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		updateData();
		mGrid.setOnItemClickListener(this);
	}

	private void updateData() {
		mFoods = getRecommenedFoods();
		mGrid.setAdapter(new MyAdapter());
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		IntakeRecordDialog dialog = IntakeRecordDialog
				.createInstance(mFoods[position].getId());
		dialog.show(getFragmentManager(), "intake_dialog");
	}

	private class MyAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return mFoods.length;
		}

		@Override
		public Food getItem(int position) {
			return mFoods[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			EatNowCardView v = (EatNowCardView) convertView;
			if (v == null) {
				v = (EatNowCardView) LayoutInflater.from(getActivity())
						.inflate(R.layout.eat_now_card, parent, false);
			}

			Food f = getItem(position);
			v.setFood(f);
			return v;
		}
	}

	private Food[] getRecommenedFoods() {
		FoodSuggestion suggestion = new FoodSuggestion(
				DataAccessor.Instance.getUser());
		return suggestion.getRecommenedFoods();
	}

	private GridView mGrid;
	private Food[] mFoods;
	Handler handler = new Handler();
	boolean dirty = false;

	DataTableListener<IntakeRecord> recordListener = new DataTableListener<IntakeRecord>() {
		@Override
		public void changed(DataTableListener.ChangeEvent<IntakeRecord> event) {
			dirty = true;
			handler.postDelayed(new Runnable() {
				@Override
				public void run() {
					if(dirty) {
						dirty = false;
						if (mGrid != null) {
							updateData();
						}
					}
				}
			}, 500);
		}
	};
	
	DataTableListener<Food> foodListener = new DataTableListener<Food>() {
		@Override
		public void changed(DataTableListener.ChangeEvent<Food> event) {
			dirty = true;
			handler.postDelayed(new Runnable() {
				@Override
				public void run() {
					if(dirty) {
						dirty = false;
						if (mGrid != null) {
							updateData();
						}
					}
				}
			}, 500);
		}
	};
}

